/*
	Figure S8 and S9: Majority White and Majority Black Districts
*/

	use data_study_2.dta, clear


// Set omitted categories
	gen zero_race = 0
	label var zero_race "White"
	gen zero_sex = 0
	label var zero_sex "Male"
	gen zero_eco = 0
	label var zero_eco "Maintain investment in energy"
	gen zero_biden = 0
	label var zero_biden "Vote Share: 51%"
	gen zero_exp = 0
	label var zero_exp "Political newcomer"
	gen zero_dist = 0
	label var zero_dist "[28, 53, 9, 6, 4]"
	gen zero_racepol = 0
	label var zero_racepol "Not shown policy"
	
// Set regression variables
	loc reg_race "cand_black zero_race cand_asian cand_hispa"
	loc reg_sex "cand_female zero_sex"
	loc reg_exp "cand_exp_teach cand_exp_council cand_exp_lawyer cand_exp_business zero_exp"
	loc reg_biden "cand_biden_p59 cand_biden_p57 cand_biden_p55 cand_biden_p53 zero_biden"
	loc reg_distpop "cand_dist1 cand_dist2 cand_dist3 cand_dist4 cand_dist5 cand_dist6 zero_dist"
	loc reg_issues "cand_policy_abort1 cand_policy_abort2 cand_policy_tax1 cand_policy_tax2"
	loc reg_issues "`reg_issues' cand_policy_health1 cand_policy_health2 cand_policy_eco1 zero_eco"
	loc reg_affirm "cand_policy_aa1 cand_policy_aa2 cand_policy_aa3 zero_racepol"
	loc reg_affirm2 "noracepolicy cand_policy_aa1 cand_policy_aa2 cand_policy_aa3 "
	
// Interactive Terms for Black Candidate X Affirmative Action
	gen noracepolicy = cand_policy_aa1 == 0 & cand_policy_aa2 == 0 & cand_policy_aa3 == 0	
	
	gen dist_white = cand_dist4 == 1 | cand_dist5 == 1 | cand_dist6 == 1 | cand_dist7 == 1 
	gen dist_black = cand_dist3 == 1
		// note baseline district is cand_dist7, [63, 8, 13, 11, 5]
		
	
	loc int ""
	foreach r in white black asian hispa {
		gen `r'_aa1 = cand_policy_aa1*cand_`r'
		gen `r'_aa2 = cand_policy_aa2*cand_`r'
		gen `r'_aa3 = cand_policy_aa3*cand_`r'
		gen `r'_aa0 = noracepolicy*cand_`r'
		
		loc lab = proper("`r'")
		if "`lab'" == "Hispa" loc lab = "Hispanic"
	
		label var `r'_aa0 "`lab' X No Position"
		label var `r'_aa1 "`lab' X Expand"
		label var `r'_aa2 "`lab' X Keep"
		label var `r'_aa3 "`lab' X End"
		
		loc int "`int' `r'_aa0 `r'_aa1 `r'_aa2 `r'_aa3"
	
	}
	// set reference
	replace white_aa0 = 0
	label var noracepolicy "Not shown position"
	
	gen out_fair_bwdiff = out_fair_black - out_fair_white

		
// Effect of candidate attributes on main outcomes, interacted
	// Pooled
	eststo clear
	reg out_ideo `reg_race' `reg_affirm' `reg_issues' `reg_sex' cand_age `reg_exp' `reg_biden' if dist_white == 1, vce(cluster r_id)	
	eststo ideo_w
	reg out_ideo `reg_race' `reg_affirm' `reg_issues' `reg_sex' cand_age `reg_exp' `reg_biden' if dist_black == 1, vce(cluster r_id)	
	eststo ideo_b
	
	reg out_fair_bwdiff `reg_race' `reg_affirm' `reg_issues' `reg_sex' cand_age `reg_exp' `reg_biden' if dist_white == 1, vce(cluster r_id)
	eststo fair_bwdiff_w
	reg out_fair_bwdiff `reg_race' `reg_affirm' `reg_issues' `reg_sex' cand_age `reg_exp' `reg_biden' if dist_black == 1, vce(cluster r_id)
	eststo fair_bwdiff_b

	coefplot (ideo_w, label() mc(teal) ciopts(color(teal) lw(med))) ///
			 (ideo_b, label() mc(gs4) ciopts(color(gs4) lw(med))) ///
				, bylabel("{bf:(a) Ideological Liberalness}") || ///
			(fair_bwdiff_w, label() mc(teal) ciopts(color(teal) lw(med))) ///
			(fair_bwdiff_b, label() mc(gs4) ciopts(color(gs4) lw(med))) ///
				, bylabel("{bf:(b) Prioritize Black over White Constituents}") ///
			|| , drop(_cons cand_age) ///
			omitted baselevels ms(c) msize(medsmall) ///
			ylabel(,labsize(vsmall)) ///
		xline(0, lc(black)) legend(order(2 "Majority-White Districts" 4 "Majority-Black Districts") size(vsmall) pos(12))  ///
		subtitle(, bcolor(white) color(black) size(vsmall)) ///
		byopts(row(1) t1title("{bf:`title'}", size(small))) ///
		xtitle("Effects of Candidate Attributes (Scale 0 to 1)", size(vsmall)) ///
		xlabel(-0.2(0.1)0.2,labsize(small)) norecycle ///
		headings(cand_black = "{bf: Race}" ///
				cand_female = "{bf: Gender}" ///
				cand_exp_teach = "{bf: Occupation}" ///
				cand_biden_p59 = "{bf: District Vote for Biden}" ///
				cand_dist4 = "{bf: District Racial % [W,B,A,H,O]}" ///
				cand_policy_abort1 = "{bf: Abortion}" ///
				cand_policy_tax1 = "{bf: Tax Policy}" ///
				cand_policy_health1 = "{bf: Healthcare}" ///
				cand_policy_eco1 = "{bf: Energy}" ///
				cand_policy_aa1 = "{bf: Affirmative Action}" ///
				, labsize(vsmall)) 
	
	graph display, xsize(4.5) ysize(3.4) margins(vsmall)	
	graph export figure_s8.png, as(png) replace
	
	
	// Interacted
	eststo clear

	reg out_ideo `reg_issues' `reg_sex' `reg_exp' `reg_biden' `int' cand_age if dist_white == 1, vce(cluster r_id)	
	eststo ideo_w
	reg out_ideo `reg_issues' `reg_sex' `reg_exp' `reg_biden' `int' cand_age if dist_black == 1, vce(cluster r_id)	
	eststo ideo_b
	
	reg out_fair_bwdiff `reg_issues' `reg_sex' `reg_exp' `reg_biden' `int' cand_age if dist_white == 1, vce(cluster r_id)
	eststo fair_bwdiff_w
	reg out_fair_bwdiff `reg_issues' `reg_sex' `reg_exp' `reg_biden' `int' cand_age if dist_black == 1, vce(cluster r_id)
	eststo fair_bwdiff_b
	
	coefplot (ideo_w, label() mc(teal) ciopts(color(teal) lw(med))) ///
			 (ideo_b, label() mc(gs4) ciopts(color(gs4) lw(med))) ///
				, bylabel("{bf:(a) Ideological Liberalness}") || ///
			(fair_bwdiff_w, label() mc(teal) ciopts(color(teal) lw(med))) ///
			(fair_bwdiff_b, label() mc(gs4) ciopts(color(gs4) lw(med))) ///
				, bylabel("{bf:(b) Prioritize Black over White Constituents}") ///
			|| , drop(_cons `reg_sex' `reg_issues' cand_age `reg_exp' `reg_biden') ///
			omitted baselevels ms(c) msize(medsmall) ///
			ylabel(,labsize(vsmall)) ///
		xline(0, lc(black)) legend(order(2 "Majority-White Districts" 4 "Majority-Black Districts") size(vsmall))  ///
		subtitle(, bcolor(white) color(black) size(vsmall)) ///
		byopts(row(1) t1title("{bf:`title'}", size(small))) ///
		xtitle("Effects of Candidate Attributes (Scale 0 to 1)", size(vsmall)) ///
		xlabel(-0.2(0.1)0.2,labsize(small)) norecycle ///
		headings(white_aa0 = "{bf: White X Affirmative Action}" ///
				black_aa0 = "{bf: Black X Affirmative Action}" ///
				asian_aa0 = "{bf: Asian X Affirmative Action}" ///
				hispa_aa0 = "{bf: Hispanic X Affirmative Action}" ///
				, labsize(vsmall)) 


	
	graph display, xsize(4.5) ysize(3.4) margins(vsmall)	

	graph export figure_s9.png, as(png) replace			
		
	